* A19a and b and A20: Ever attrition attrition dummy ex-post

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear


drop if add_sample_w2==1 | add_sample_w5==1  

gen cashcrop=0
replace cashcrop=1 if cotton_k==1|castor_k==1|groundnut_k==1

replace flyer_hyv=1 if flyer_hyv_exp==1

** creating demographic variables

gen educ_head=.
gen age_head=.
forvalues a=1/18 {
replace educ_head=a18_c`a' if a13_c`a' == 1
replace age_head=a14_c`a' if a13_c`a' == 1
replace educ_head=0 if educ_head==888
replace educ_head=0 if educ_head==15 | educ_head==16
}

replace sex=0 if sex==2
rename sex male
lab var male "=1 if male"

foreach var of varlist age_head educ_head male wealth_index hh_size {
bys id: replace `var'=`var'[_n+1] if `var'==. 
}

gsort id wave
gen w1_age_head = age_head if mkt_year==2005
replace w1_age_head = age_head if mkt_year==2008 & firstt_2009==1

gen w1_head_educ = educ_head if mkt_year==2005
replace w1_head_educ = educ_head if mkt_year==2008 & firstt_2009==1


gen w1_male=male if mkt_year==2005
replace w1_male=male if mkt_year==2008 & firstt_2009==1

cap drop finlit_new
egen finlit_new=rowmean(l21c l22c l23c l32c l32c l33c l34c l35c l36c l41c l42c l43c l44c)


gen w1_hasplot = hasplot if mkt_year==2005 & firstt_2009==0
replace w1_hasplot=hasplot if mkt_year==2008 & firstt_2009==1

gen w1_finlit = finlit_new if mkt_year==2005 & firstt_2009==0
replace w1_finlit=finlit_new if mkt_year==2008 & firstt_2009==1

gen w1_wealth_index = wealth_index if mkt_year==2005 & firstt_2009==0
replace w1_wealth_index = wealth_index if mkt_year==2008 & firstt_2009==1

gen w1_hh_size= hh_size if mkt_year==2005 & firstt_2009==0
replace w1_hh_size = hh_size if mkt_year==2008 & firstt_2009==1

gen w1_mainY_agrown = mainY_agrown if mkt_year==2005 & firstt_2009==0
replace w1_mainY_agrown = mainY_agrown if mkt_year==2008 & firstt_2009==1

gen w1_mainY_agrlabor = mainY_agrlabor if mkt_year==2005 & firstt_2009==0
replace w1_mainY_agrlabor = mainY_agrlabor if mkt_year==2008 & firstt_2009==1

gen w1_cashcrop = cashcrop if mkt_year==2005 & firstt_2009==0
replace w1_cashcrop = cashcrop if mkt_year==2008 & firstt_2009==1

gen w1_plotsize = plotsize_ha if mkt_year==2005 & firstt_2009==0
replace w1_plotsize = plotsize_ha if mkt_year==2008 & firstt_2009==1

gen w1_consumption = real_consumption if mkt_year==2005 & firstt_2009==0
replace w1_consumption = real_consumption if mkt_year==2008 & firstt_2009==1

gen w1_cost_k_w1 = cost_k_w1 if mkt_year==2005 & firstt_2009==0
replace w1_cost_k_w1 = cost_k_w1 if mkt_year==2008 & firstt_2009==1

foreach x of varlist w1_cashcrop w1_mainY_agrown w1_mainY_agrlabor  ///
w1_hh_size w1_wealth_index w1_plotsize w1_male w1_head_educ w1_age_head w1_consumption  {
bys id : egen m_`x'= min(`x') 
}

replace m_w1_cashcrop=0 if m_w1_cashcrop==.

foreach x of varlist   ///
m_w1_hh_size m_w1_wealth_index  m_w1_plotsize m_w1_male m_w1_head_educ m_w1_age_head  {
gen miss_`x'=0
replace miss_`x'=1 if `x'==.
replace `x'=0 if `x'==.
}

* original treatment 

gen t1=0
replace t1=1 if originaltvillage==1


local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

log using ../res/ever-attrit-ex-post, replace
gen EP_A=0
replace EP_A=1 if ex_post_balp==0

bys id : egen ever_attrit=max(EP_A)

/* treatment regressions */

local col=1
	di "`table'"
* reduced form

	reg ever_attrit t1 firstt_2007 firstt_2009 resurveyed year2006-year2013 if firstt_2009==0, r cluster(villageno)
	test t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'
	predict treat_ols_v, xb
	
	reg ever_attrit `uniqinst' t1 firstt_2007 firstt_2009 resurveyed year2006-year2013, r cluster(villageno)
	test `uniqinst' t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'
	predict treat_ols_i, xb
	
	probit ever_attrit t1 firstt_2007 firstt_2009 resurveyed year2006-year2013 if firstt_2009==0, r cluster(villageno)
	test t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'
	predict treat_prob_v, pr
	
	probit ever_attrit `uniqinst' t1 firstt_2007 firstt_2009 resurveyed year2006-year2013, r cluster(villageno)
	test `uniqinst' t1 firstt_2007 firstt_2009
	estimates store cT`table'C`col++'
	predict treat_prob_i, pr
	
	qui estout cT`table'C* using "../out/everatt-treat-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats(F N)

* IV regressions

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

local col=1
	di "`table'"
	
	* Village IV (No Fixed Effects) 
	ivreg2 ever_attrit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed if firstt_2009==0, r cluster(villageno)
	test s_policy_units firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_iv_v, xb 

	/* IND IV (No Fixed Effects) */
	ivreg2 ever_attrit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 , r  cluster(villageno)
	test s_policy_units firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict treat_iv_i, xb
	
	qui estout cT`table'C* using "../out/everatt-treat-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N)
	
local col=1
	di "`table'"
	
	* Village IV (No Fixed Effects) 
	ivprobit ever_attrit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed if firstt_2009==0, r cluster(villageno)
	test s_policy_units firstt_2007 
	estimates store cT`table'C`col++'
	predict treat_ivprobit_v, pr 

	/* IND IV (No Fixed Effects) */
	ivprobit ever_attrit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 , r  cluster(villageno)
	test s_policy_units firstt_2007 firstt_2009
	estimates store cT`table'C`col++'	
	predict treat_ivprobit_i, pr
	
	qui estout cT`table'C* using "../out/everatt-treat-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(chi2 N)

/* baseline regressions */
local col=1
	di "`table'"
	
	reg ever_attrit m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop miss_m_w1* , r cluster(villageno)
	test m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop
	estimates store cT`table'C`col++'
	predict bas_attrit_ols, xb
	
	/*Probits */
	probit ever_attrit m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop miss_m_w1*  , r cluster(villageno)
	test m_w1_male m_w1_age_head m_w1_head_educ m_w1_hh_size m_w1_wealth_index m_w1_consumption m_w1_plotsize m_w1_cashcrop
	estimates store cT`table'C`col++'
	predict bas_attrit_probit, pr 
	
	qui estout cT`table'C* using "../out/everatt-bas-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats(N)
	
drop miss_*

	
la var bas_attrit_ols "Predicted attrition from baseline characteristics - OLS"
la var bas_attrit_probit "Predicted attrition from baseline characteristics - probit"


foreach var of varlist bas_attrit_ols bas_attrit_probit  {
sum `var'
local l`var': variable label `var'
hist `var', percent xtitle("predicted attrition rate") ytitle("percent") title("`l`var''", size(small))
graph save ../out/`var'.gph, replace
}

grc1leg ../out/bas_attrit_ols.gph ../out/bas_attrit_probit.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../out/bas-attrition-expost.emf", replace


foreach x of varlist treat_ols_v treat_ols_i treat_prob_v treat_prob_i treat_iv_v treat_iv_i treat_ivprobit_v treat_ivprobit_i bas_attrit_ols bas_attrit_probit {
sum `x'
gen inv_`x'=1/`x'
}

log close 

replace flyer_hyv=1 if flyer_hyv_exp==1

** Don't include flyer_exp in marketing assignment

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

keep if ex_post_balp==1

drop if id==111246 | id==113474
local table = 1
local rastring "replace"
local outcomevars "w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std w_real_cons"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

xtset id mkt_year


*a19a
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_prob_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_prob_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_treat_prob_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_treat_prob_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Et3-treat-probit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a19b
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_ivprobit_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_treat_ivprobit_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_treat_ivprobit_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_treat_ivprobit_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Et3-treat-ivprobit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a20
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_bas_attrit_probit] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_bas_attrit_probit] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed  firstt_2009 [pw=inv_bas_attrit_probit] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed firstt_2009 [pw=inv_bas_attrit_probit], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/Et3-bas-probit-invp.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

